package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * description
 *
 * @author SZ
 * @date 2022/9/25/11:51
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {
    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;
    @Autowired
    private ReturnListService returnListService;
    @Autowired
    private GoodsService goodsService;

    @Override
    @Transactional
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr) {
        //插入记录到表:t_return_list
        returnListService.insertReturnList(returnList);
        Gson gson = new Gson ();
        List list = gson.fromJson (returnListGoodsStr, List.class);
        for (Object o : list) {
            String json = gson.toJson (o);
            ReturnListGoods returnListGoods = gson.fromJson (json, ReturnListGoods.class);
            returnListGoods.setReturnListId (returnList.getReturnListId ());
            //插入记录到表:t_return_list_goods
            returnListGoodsDao.insertReturnListGoods(returnListGoods);
            //修改库存数量:t_goods
            Integer goodsNum = returnListGoods.getGoodsNum ();
            Integer goodsId = returnListGoods.getGoodsId ();
            goodsService.minusQuantity (goodsNum,goodsId);
        }
        return new ServiceVO (SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<ReturnListGoods> getGoodsList(Integer returnListId) {
        List<ReturnListGoods> goodsList = null;
        if (returnListId!=null){
            goodsList = returnListGoodsDao.getGoodsList (returnListId);
        }

        return goodsList;
    }
}
